package com.fw.application.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.application.controller.base.UserController;
import com.fw.common.IdXD;
import com.fw.mes.Result;
import com.fw.system.web.model.entity.TbAddress;
import com.fw.system.web.model.entity.TbConutrySort;
import com.fw.system.web.model.entity.TbSysRule;
import com.fw.system.web.service.ITbAddressService;
import com.fw.system.web.service.ITbConutrySortService;
import com.fw.system.web.service.ITbSysRuleService;
import com.fw.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.stream.Collectors;

import static com.fw.mes.ResultUtils.success;

/**
 * <p>
 * 用户收货信息地址表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-04-14
 */
@RestController
@RequestMapping("/web/address")
@CrossOrigin
@Api(tags = "收货信息")
public class TbAddressController implements UserController {

    @Autowired
    private ITbAddressService addressService;
    @Autowired
    private IdXD idXD;
    @Autowired
    private ITbSysRuleService ruleService;
    @Autowired
    private ITbConutrySortService conutrySortService;

    @GetMapping("/CheckSetAddrInfo")
    @ApiOperation("首页完善信息检查接口，点击先调用这个接口检查 true就跳转收货地址页面完善 false去求啥也不让干提示不符合条件")
    public Result CheckSetAddrInfo() {

        TbSysRule startAndEndTime = ruleService.getById(8);
        TbAddress one = addressService.getOne(Wrappers.<TbAddress>lambdaQuery().eq(TbAddress::getUserId, this.getUser().getId()));
        if (LocalDateTime.now().compareTo(startAndEndTime.getEndTime()) >= 0) {
            //检查是不是一千名
            int i = conutrySortService.list(Wrappers.<TbConutrySort>lambdaQuery()
                    .orderByDesc(TbConutrySort::getCountFool)
                    .orderByAsc(TbConutrySort::getCountTime))
                    .stream().map(map -> map.getUserId())
                    .collect(Collectors.toList())
                    .indexOf(this.getUser().getId());
            if (one == null) {
                if (i >= 0 && i < 1000)
                    return success(2);
            }
        } else {
            if (one == null)
                return success(1);
        }
        return success(-1);

    }

    @GetMapping("/getAddress")
    @ApiOperation("获取当前用户的收货地址信息")
    public Result getAddress() {
        TbAddress one = addressService.getOne(Wrappers.<TbAddress>lambdaQuery().eq(TbAddress::getUserId, this.getUser().getId()));
        return success(one);

    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加或者修改")
    public Result addOrUpdate(@RequestBody TbAddress address) {


        if (StringUtils.isEmpty(address.getId())) {
            address.setId(idXD.nextId());
            address.setUserId(this.getUser().getId());
        }
        addressService.saveOrUpdate(address);
        return success();
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable String id) {
        return success(addressService.getBaseMapper().deleteById(id));
    }


}

